/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.employee.service;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.chlitina.store.common.persistence.Page;
import com.chlitina.store.common.service.BaseService;
import com.chlitina.store.modules.employee.dao.StoreBonusSettingProductDao;
import com.chlitina.store.modules.employee.entity.Employee;
import com.chlitina.store.modules.employee.entity.StoreBonusSettingProduct;
import com.chlitina.store.modules.employee.entity.StoreBonusSettingTemplateProduct;
import com.chlitina.store.modules.sys.utils.StoreUtils;

/**
 * 产品销售提成设定Service
 * @author liujiang
 * @version 2013-11-19
 */
@Component
@Transactional(readOnly = true)
public class StoreBonusSettingProductService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(StoreBonusSettingProductService.class);
	
	@Autowired
	private StoreBonusSettingProductDao storeBonusSettingProductDao;
	@Autowired
	private StoreBonusSettingTemplateProductService storeBonusSettingTemplateProductService;
	
	public StoreBonusSettingProduct get(Long id) {
		return storeBonusSettingProductDao.findOne(id);
	}
	
	public Page<StoreBonusSettingProduct> find(Page<StoreBonusSettingProduct> page, Long employeeId, Integer type) {
		DetachedCriteria dc = storeBonusSettingProductDao.createDetachedCriteria();
		dc.add(Restrictions.eq("employee.id", employeeId));
		dc.add(Restrictions.eq("card_type", type));
		dc.addOrder(Order.desc("id"));
		return storeBonusSettingProductDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(StoreBonusSettingProduct storeBonusSettingProduct) {
		storeBonusSettingProductDao.save(storeBonusSettingProduct);
	}
	
	@Transactional(readOnly = false)
	public void delete(StoreBonusSettingProduct storeBonusSettingProduct) {
		storeBonusSettingProductDao.delete(storeBonusSettingProduct);
	}
	
	public List<StoreBonusSettingProduct> getByEmployee(Employee employee) {
		DetachedCriteria dc = storeBonusSettingProductDao.createDetachedCriteria();
		dc.add(Restrictions.eq("store", StoreUtils.getStore()));
		dc.add(Restrictions.eq("employee", employee));
		return storeBonusSettingProductDao.find(dc);
	}
	
	public List<Long> getExistTemplateId(long employeeId, int productType, int cardType) {
		return storeBonusSettingProductDao.getExistTemplateId(StoreUtils.getStore().getId(), employeeId, productType, cardType);
	}
	
	@Transactional(readOnly = false)
	public void copy(Employee employee, int ifModify) {
		List<StoreBonusSettingTemplateProduct> list = storeBonusSettingTemplateProductService.getByStore();
		List<StoreBonusSettingProduct> existList = getByEmployee(employee);
		for (StoreBonusSettingTemplateProduct stp : list) {
			StoreBonusSettingProduct same = null;
			for (StoreBonusSettingProduct sp : existList) {
				if(stp.getProduct_type() == sp.getProduct_type()
						&& stp.getProduct_category_id() == sp.getProduct_category_id()
						&& stp.getCard_type() == sp.getCard_type()) {
					same = sp;
					break;
				}
			}
			if(same != null) {
				if(ifModify == 1) {
					same.setBonus_type(stp.getBonus_type());
					same.setBonus_rate(stp.getBonus_rate());
					same.setFixed_money(stp.getFixed_money());
					save(same);
				}
			} else {
				StoreBonusSettingProduct storeBonusSettingProduct = new StoreBonusSettingProduct();
				storeBonusSettingProduct.setStore(stp.getStore());
				storeBonusSettingProduct.setEmployee(employee);
				storeBonusSettingProduct.setProduct_type(stp.getProduct_type());
				storeBonusSettingProduct.setProduct_category_id(stp.getProduct_category_id());
				storeBonusSettingProduct.setCard_type(stp.getCard_type());
				storeBonusSettingProduct.setBonus_type(stp.getBonus_type());
				storeBonusSettingProduct.setBonus_rate(stp.getBonus_rate());
				storeBonusSettingProduct.setFixed_money(stp.getFixed_money());
				save(storeBonusSettingProduct);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void copy(Employee employee, int ifModify, Employee from) {
		List<StoreBonusSettingProduct> list = getByEmployee(from);
		List<StoreBonusSettingProduct> existList = getByEmployee(employee);
		for (StoreBonusSettingProduct stp : list) {
			StoreBonusSettingProduct same = null;
			for (StoreBonusSettingProduct sp : existList) {
				if(stp.getProduct_type() == sp.getProduct_type()
						&& stp.getProduct_category_id() == sp.getProduct_category_id()
						&& stp.getCard_type() == sp.getCard_type()) { 
					same = sp;
					break;
				}
			}
			if(same != null) {
				if(ifModify == 1) {
					same.setBonus_type(stp.getBonus_type());
					same.setBonus_rate(stp.getBonus_rate());
					same.setFixed_money(stp.getFixed_money());
					save(same);
				}
			} else {
				StoreBonusSettingProduct storeBonusSettingProduct = new StoreBonusSettingProduct();
				storeBonusSettingProduct.setStore(stp.getStore());
				storeBonusSettingProduct.setEmployee(employee);
				storeBonusSettingProduct.setProduct_type(stp.getProduct_type());
				storeBonusSettingProduct.setProduct_category_id(stp.getProduct_category_id());
				storeBonusSettingProduct.setCard_type(stp.getCard_type());
				storeBonusSettingProduct.setBonus_type(stp.getBonus_type());
				storeBonusSettingProduct.setBonus_rate(stp.getBonus_rate());
				storeBonusSettingProduct.setFixed_money(stp.getFixed_money());
				save(storeBonusSettingProduct);
			}
			
		}
	}
}
